<?php

/*****************************************************************************
 * 
 *  Copyright 2009 Sylvain Dudoit (contact : sylvain.dudoit at gmail.com)
 *  
 *  This file is part of Poupitchi.
 *
 *  Poupitchi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Poupitchi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Poupitchi.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/


///////////////////////////////////////////////////////////////////////////////
// Start session

session_start();

///////////////////////////////////////////////////////////////////////////////
// Define data location

$P_DATA = "data/";

///////////////////////////////////////////////////////////////////////////////
// Include dependencies

require_once($P_DATA."config.php");
require_once("library/utils.php");
require_once("languages/".P_LANG.".php");
require_once("library/modules/users/db.".P_DB.".php");

///////////////////////////////////////////////////////////////////////////////
// Login action requested

if ($_GET['action'] == "login") {
    // get post
    $plogin    = $_POST['login'];
    $ppassword = $_POST['password'];
    // check
    if (checkUser($plogin, $ppassword)) {
        $_SESSION['P_USER'] = $plogin;
    } else {
        $P_ERROR = tr("Invalid authentication !");
    }
}

///////////////////////////////////////////////////////////////////////////////
// Logout action requested

if ($_GET['action'] == "logout") {
    session_unset($_SESSION['P_USER']);
    session_register('P_DATA', $P_DATA);    
}

///////////////////////////////////////////////////////////////////////////////
// Set user logged and show appropriate page

$P_USER   = $_SESSION['P_USER'];
$P_ADMIN  = $P_USER === "admin";
$P_MODULE = isset($_GET['module']) ? $_GET['module'] : $P_MODULES[0];

if (isset($_SESSION['P_USER'])) {
  require_once("library/area_private.php");
} else {
  require_once("library/area_public.php");
}

?>